package com.hunanwenli.controller.shop;

import com.alibaba.fastjson.JSON;
import com.hunanwenli.entity.Content;
import com.hunanwenli.entity.OrderMsgVo;
import com.hunanwenli.entity.ShopGoods;
import com.hunanwenli.entity.UserInfo;
import com.hunanwenli.service.login.FeignLoginService;
import com.hunanwenli.service.sk.FeignSkService;
import com.hunanwenli.service.userInfo.FeignUserInfoService;
import com.hunanwenli.tool.FileTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * @author : wanzhenyu
 * @Description:
 * @date Date : 2022/3/18
 */
@Controller
public class ShopController extends BaseController {
    @Autowired
    private FeignSkService feignSkService;
    @GetMapping(value = "/admin")
    public String toTest(){
        return "test";
    }


    @RequestMapping(value = "/addDiscountGoods" ,method = RequestMethod.POST)
    @ResponseBody
    public String addDiscountGoods(Long uid,
                                   Long time,
                                   String goodName,
                                   Integer goodStoke,
                                   Double goodPrice,
                                   MultipartFile file) throws IOException {
        if (file==null){
            throw new RuntimeException("文件为空");
        }
        FileTypeEnum.checkFileType(Objects.requireNonNull(file.getOriginalFilename()), FileTypeEnum.FILE_TU_JPG.getSuffix());
        FileTypeEnum.checkFileType(Objects.requireNonNull(file.getOriginalFilename()), FileTypeEnum.FILE_TU_PNG.getSuffix());
        String filepath = "D:\\ideaProject\\shopSeckill\\008-static\\src\\main\\webAppPhoto\\";
        String uuid= UUID.randomUUID().toString().substring(0,5);
        String newFileName =filepath+uuid+ file.getOriginalFilename();
        File file1 = new File(newFileName);
        file.transferTo(file1);
        return feignSkService.addDiscountGoods(uid,time,goodName,goodStoke,goodPrice,uuid+ file.getOriginalFilename());

    }
    @GetMapping(value = "allGoods")
    public String allGoods(Model model, HttpServletRequest request){
        getLoginUser(model,request);
        String allGoodsStr = feignSkService.allGoods();
        List<ShopGoods> shopGoods = JSON.parseArray(allGoodsStr,ShopGoods.class);
        model.addAttribute("shopGoods",shopGoods);
        return "shop";
    }

    @GetMapping(value = "searchShop")
    public String searchShop(Model model, HttpServletRequest request,String shopName){
        getLoginUser(model,request);
        if (StringUtils.isEmpty(shopName)){
            throw new RuntimeException("搜索为空");
        }
        String goods = feignSkService.searchShop(shopName);
        List<Content> goodsList = JSON.parseArray(goods, Content.class);
        model.addAttribute("goodsList",goodsList);
        return "shop/SearchGoodsResult";
    }

    @GetMapping(value = "fastShop")
    public String fastShop(Model model, HttpServletRequest request){
        getLoginUser(model,request);
        String goods = feignSkService.fastShop();
        List<Content> goodsList = JSON.parseArray(goods, Content.class);
        model.addAttribute("goodsList",goodsList);
        return "shop/SearchGoodsResult";
    }
    @GetMapping(value = "groupShop")
    public String groupShop(Model model, HttpServletRequest request){
        getLoginUser(model,request);
        String goods = feignSkService.groupShop();
        List<Content> goodsList = JSON.parseArray(goods, Content.class);
        model.addAttribute("goodsList",goodsList);
        return "shop/TuanGoodsResult";
    }

    @GetMapping(value = "myOrderMsg")
    public String myOrderMsg(Model model, HttpServletRequest request){
        UserInfo loginUser = getLoginUser(model, request);
        String phone = loginUser.getPhone();
        String myOrderMsg = feignSkService.myOrderMsg(phone);
        List<OrderMsgVo> orderMsgVos = JSON.parseArray(myOrderMsg, OrderMsgVo.class);
        model.addAttribute("orderMsgVos",orderMsgVos);
        return "shop/OrderResult";
    }
    @GetMapping(value = "seckill")
    public String secKill(Long id,Model model ,HttpServletRequest request){
        UserInfo loginUser = getLoginUser(model, request);
        String result = feignSkService.seckill(id, loginUser.getPhone());
        model.addAttribute("result",result);
        return "shop/shopResult";
    }
}
